/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.renderer;

import java.awt.Component;
import java.util.Arrays;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


/**
 * PasswordTableCellRenderer
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class PasswordTableCellRenderer extends DefaultTableCellRenderer {

  private static final long serialVersionUID = 143048113628503320L;
  private boolean showPassword = false;
  
  /**
   * Constructor
   *
   * @param showPassword
   */
  public PasswordTableCellRenderer(boolean showPassword) {
    this.showPassword = showPassword;
  }
  
  /* (non-Javadoc)
   * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
   */
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
    if (showPassword) {
      this.setText(value != null ? String.valueOf(value) : "");
    } else {
      int len = value != null ? value.toString().length() : 1;
      char[] password = new char[len];
      Arrays.fill(password, '*');
      this.setText(String.valueOf(password));
    }
    return this;
  }

  
  /**
   * isShowPassword
   *
   * @return Returns the showPassword
   */
  public boolean isShowPassword() {
    return showPassword;
  }

  
  /**
   * setShowPassword
   *
   * @param showPassword The showPassword to set.
   */
  public void setShowPassword(boolean showPassword) {
    this.showPassword = showPassword;
  }

  
  
}
